<?php

require_once(t3lib_extMgm::extPath('zlib') . 'class.tx_zlib_abstract_viewhelper.php');

class tx_zlib_viewhelper
{

    /**
     * @var array
     */
    private $helpers = array();

    /**
     * @var array
     */
    private $paths = array();

    /**
     * @var tx_zlib_view
     */
    private $view;

    /**
     * @var tslib_pibase
     */
    private $pi;

    /**
     * @param tslib_pibase $pi
     */
    public function __construct(tslib_pibase $pi)
    {
    	$this->pi = $pi;
    }

    /**
     * @param tx_zlib_view $view
     */
    public function setView(tx_zlib_view $view)
    {
        $this->view = $view;
    }

    /**
     * @return tx_zlib_view
     */
    public function getView()
    {
        return $this->view;
    }

    /**
     * @param tslib_pibase $pi
     */
    public function setPi(tslib_pibase $pi)
    {
		$this->pi = $pi;
    }

    /**
     * @return tslib_pibase
     */
    public function getPi()
    {
        return $this->pi;
    }

    /**
     * @param string $helper
     * @return unknown
     */
    protected function loadViewHelper($helper)
    {
        if (!isset($this->helpers[$helper])) {
            foreach($this->paths AS $path) {
    			$className	= $path['prefix'] . $helper;
    			$fileName	= $path['path'] . '/class.' . $className . '.php';
                if (file_exists($fileName)) {
                    include_once $fileName;
                    $helperObject = new $className();
                    $helperObject->setPi($this->pi);
                    $helperObject->setView($this->view);
                    $this->helpers[$helper] = $helperObject;
                    break;
                }
            }
        }
        if(isset($this->helpers[$helper])) {
        	return $this->helpers[$helper];
        }
        $paths = array();
        foreach($this->paths AS $path) {
        	$paths[] = $path['path'];
        }
        throw new Exception("Helper $helper not found in " . implode(",", $paths));
    }

    /**
     * @param string $path
     * @param string $prefix
     */
    public function addPath($path, $prefix)
    {
        $helperPath = array(
            'path' => rtrim($path, '/'),
            'prefix' => $prefix
        );
        array_unshift($this->paths, $helperPath);
    }

    /**
     * @return array
     */
    public function getPaths()
    {
        return $this->paths;
    }

    /**
     * @param string $methodName
     * @param mixed $args
     * @return mixed
     */
    public function __call($methodName, $args)
    {
        $helper = $this->loadViewHelper($methodName);
        if($helper instanceof tx_zlib_abstract_viewhelper) {
            return call_user_func_array(
                array($helper, $methodName),
                $args
            );
        }
    }

    /**
     * @param string $value
     * @return string
     */
    public function escape($value)
    {
    	return htmlspecialchars($value);
    }


	/**
	 * @return int Current SysLanguageUid
	 */
	public function getLanguageId() {
		return intval($GLOBALS['TSFE']->sys_language_uid);
	}

	/**
	 * @return string
	 */
	public function extPath()
	{
	    $extKey = $this->pi->getExtKey();
	    return t3lib_extMgm::extPath($extKey);
	}

	/**
	 * @return string
	 */
	public function extRelPath()
	{
	    $extKey = $this->pi->getExtKey();
	    return t3lib_extMgm::extRelPath($extKey);
	}

	/**
	 * @return string
	 */
	public function siteRelPath()
	{
	    $extKey = $this->pi->getExtKey();
	    return t3lib_extMgm::siteRelPath($extKey);
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_viewhelper.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_viewhelper.php']);
}
